package cn.ekuma.data.util;

import android.os.AsyncTask;

import com.openbravo.data.basic.BasicException;

import java.util.concurrent.ExecutionException;

import cn.ekuma.data.dao.I_DataLogic;
import cn.ekuma.data.dao.bean.I_BaseBean;

/**
 * Created by Administrator on 2014/12/19.
 */
public class DataLogicUtil {
    public static int async_delete(final I_DataLogic dl,I_BaseBean obj){
        try {
            return  new AsyncTask<I_BaseBean,Integer,Integer>(){
                @Override
                protected Integer doInBackground(I_BaseBean... params) {
                    try {
                        return dl.delete(params[0]);
                    } catch (BasicException e) {
                        e.printStackTrace();
                    }
                    return -1;
                }
            }.execute(obj).get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return -1;
    }


    public static int async_insert(final I_DataLogic dl,I_BaseBean obj){
        try {
            return  new AsyncTask<I_BaseBean,Integer,Integer>(){
                @Override
                protected Integer doInBackground(I_BaseBean... params) {
                    try {

                        return dl.insert(params[0]);
                    } catch (BasicException e) {
                        e.printStackTrace();
                    }
                    return -1;
                }
            }.execute(obj).get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return -1;
    }


    public static int async_update(final I_DataLogic dl,I_BaseBean obj){
        try {
            return  new AsyncTask<I_BaseBean,Integer,Integer>(){
                @Override
                protected Integer doInBackground(I_BaseBean... params) {
                    try {
                        return dl.update(params[0]);
                    } catch (BasicException e) {
                        e.printStackTrace();
                    }
                    return -1;
                }
            }.execute(obj).get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return -1;
    }


}
